<template>
	<view>
		<cu-custom :bgcolor="'#fff'" />
		<view class="search-box p-2">
			<view class="input" @click="showDrawer('showLeft')">
				<view class="iconfont icon-sousuo main-text-color"></view>
				<input class="uni-input search-input" style="padding-left: 80rpx;" disabled placeholder="请输入搜索内容" />
				<text class="font-md main-text-color search">搜索</text>
			</view>
		</view>
		<!-- 搜索栏的弹出层 -->
		<uni-drawer ref="showLeft" mode="left" :width="280" @change="change($event,'showLeft')">
			<view class="p-2" style="padding-top: 200rpx;">
				<uni-forms :modelValue="searchForm" label-position="top" ref="form">
					<uni-forms-item label="关键字" name="name" label-width="120">
						<uni-easyinput type="text" v-model="searchForm.keyword" placeholder="请输入搜索内容" />
					</uni-forms-item>
				</uni-forms>
				<view class="button-box mt-4">
					<button class="main-bg-color text-white font-md" @click="searchList">搜索</button>
				</view>
			</view>
		</uni-drawer>
		<view v-if="shopList && shopList.length >0">
			<scroll-view scroll-y="true" :style="'height:'+scorllHeight+ 'px;'" >
				<view class="shop-list">
					<view class="list-box pt-3 pb-3 pl-4 pr-4 mb-2" v-for="(item, index) in shopList" :key="index">
						<view class="list">
							<view class="list-header-info d-flex flex-row">
								<image class="mr-2"
									style="width: 150rpx; height: 150rpx; background-color: #eeeeee; border-radius: 5rpx;"
									mode="scaleToFill" :src="returnImg(item.original_img)"></image>
								<view class="d-flex flex-column flex-1">
									<view class="" style="font-size: 32rpx;">
										{{item.goods_name}}(编号：{{index + 1}})
									</view>
									<view class="d-flex flex-row  span-20" style="font-size: 26rpx;color: #999999;">
										<text class="mr-2 font-md main-text-color span-15">现价
											￥{{item.shop_price}}</text>
										<text class="span-15">原价 ￥{{item.original_price}}</text>
									</view>
									<view class="d-flex flex-row font-md" style="color: #999999;">
										<text class="mr-2 span-15">重量 {{item.xfxz_goods_weight}}KG</text>
										<text class="main-text-color span-15">库存 {{item.goods_number}}</text>
									</view>
								</view>
							</view>
							<view class="list-center-info span-20 d-flex flex-column mt-2">
								<view class="d-flex flex-row  span-20" style="color: #999999;">
									<text class="mr-2 font-md span-20">货号: {{item.goods_sn}}</text>
									<!-- <text class="flex-1 font-md">排序: {{item.sort_order}}</text> -->
								</view>
								<view class="d-flex flex-row font-md" style="color: #999999;">
									<text class="mr-2 span-8 font-md">权重:{{item.sort_order}}</text>
									<text class="flex-1 font-md">是否上架: <text
											class="main-text-color">{{item.is_on_sale == 0? '否':'是'}}</text></text>
								</view>
								<view class="d-flex flex-row  span-20" style="color: #999999;">
									<text class="flex-1 font-md">是否显示预估到手价:<text
											class="main-text-color">{{item.is_xsdsj == 0 ?'否':'是'}}</text></text>
								</view>
							</view>
						</view>
						<view class="list-btn mt-3 d-flex flex-row a-center j-end">
							<button class="mini-btn m-0 mr-2 input-border-color border bg-none main-text-color"
								type="default" size="mini" @click="goodSale(item.goods_id,item.is_on_sale)">{{item.is_on_sale == 0 ? '上架' : '下架' }}</button>
							<button class="mini-btn m-0 main-bg-color text-white" type="default" size="mini"
								@click.stop="toDetail(item.goods_id)">编辑</button>
						</view>
					</view>

				</view>
				<!-- <view class="d-flex a-center j-center text-light-muted font-md py-3">{{loadtext}}</view> -->
			</scroll-view>
		</view>
		<view v-else class="text-center mt-5">
			您还没有添加商品, <text class="main-text-color" @click="toAddShop">点击前去添加</text>
		</view>
	</view>
</template>

<script>
	import title from "@/components/common/title.vue"
	import divider from "@/components/common/divider.vue"
	import cuCustom from '@/components/common/custom.vue'
	import interceptor from "@/utils/request.js"
	export default {
		components: {
			title,
			divider,
			cuCustom
		},
		data() {
			return {
				value: 0,
				range: [{
						value: 0,
						text: "分类"
					},
					{
						value: 1,
						text: "二两蟹二两蟹"
					},
					{
						value: 2,
						text: "一两蟹"
					},
				],
				scorllHeight: 500,
				shopList: [],
				loadtext: "上拉加载更多",
				allChecked: false,
				searchForm: {
					keyword: ""
				},
				getApp:""
			}
		},
		onLoad() {
			uni.getSystemInfo({
				success: (res => {
					this.scorllHeight = res.windowHeight - uni.upx2px(350)
				})
			})
			this.getApp = getApp()
		},
		onShow() {
			this.getShopList()
		},
		methods: {
			onKeyInput(e) {
				console.log(e.detail.value)
			},
			change(e) {
				console.log("e:", e);
			},
			// 上拉加载更多
			loadMore() {
				if (this.loadtext !== '上拉加载更多') return
				this.loadtext = "加载中..."
				setTimeout(() => {
					// this.shopList = [...this.shopList, ...this.addlist]
					// 回复状态
					this.loadtext = "上拉加载更多"
				}, 2000)

			},
			toDetail(id) {
				uni.navigateTo({
					url: "./addShop?id=" + id
				})
			},
			toAddShop() {
				uni.navigateTo({
					url: './addShop'
				})
			},
			// 下架
			goodSale(goodId, status){
				let _that = this
				let params = {
					supplier_id: uni.getStorageSync('supplier_id'),
					id: goodId,
					val: status == 0? 0 : 1,
					Is_ajax: 1
				}
				interceptor.get('/supplier_applet_api.php?act=goodsSale', params).then((res) => {
					if(res.status === 1){
						uni.showToast({
							title: res.msg,
							icon:"success",
							duration:1000,
							success() {
								
							}
						})
						setTimeout(() => {
							_that.getShopList()
						},1000)
					}else {
						uni.showToast({
							title: res.msg,
							icon:"none"
						})
					}
				})
				
			},
			// 删除功能暂时不做
			// toDelete(index) {
			// 	let _that = this
			// 	interceptor.get('/supplier_applet_api.php?act=removeGoods',{id}).then((res) => {
			// 		if(res.status === 1){
			// 			uni.showToast({
			// 				title:"删除成功",
			// 				icon:'success',
			// 				success() {
			// 					_that.getShopList()
			// 				}
			// 			})
			// 		}else{
			// 			uni.showToast({
			// 				title: res.msg,
			// 				icon:'error',
			// 			})
			// 		}
			// 	})
			// },
			showDrawer(e) {
				this.$refs[e].open()
			},
			// 关闭窗口
			closeDrawer(e) {
				this.$refs[e].close()
			},
			// 抽屉状态发生变化触发
			change(e, type) {
				console.log((type === 'showLeft' ? '左窗口' : '右窗口') + (e ? '打开' : '关闭'));
				this[type] = e
			},
			//  获取商品列表
			getShopList() {
				let params = {
					supplier_id: uni.getStorageSync("supplier_id"),
					supplier_status: 1
				}
				interceptor.get('/supplier_applet_api.php?act=goodsList', params).then((res) => {
					if (res.status === 1) {
						const {
							info
						} = res
						this.shopList = info.goods_list
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			},
			returnImg(img) {
				return `${getApp().globalData.baseUrl}/${img}`
			},
			// 根据条件筛选
			searchList(){
				let params = Object.assign({},{
					supplier_id: uni.getStorageSync("supplier_id")
				},this.searchForm)
				interceptor.get("/supplier_applet_api.php?act=selGoods",params).then((res) =>{
					if (res.status === 1) {
						const {
							info
						} = res
						this.shopList = info.goods_list
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.search-box {
		width: 100%;
		height: 160upx;
		background: #fff;

		box-sizing: border-box;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;

		.input {
			width: 100%;
			border-radius: 20px;
			overflow: hidden;
			border: 1px solid #BBBBBB;
			position: relative;

			.icon-sousuo {
				position: absolute;
				left: 35upx;
				top: 10upx;
			}

			.search {
				position: absolute;
				right: 40upx;
				top: 8upx;
			}
		}
	}

	.shop-list {
		background: #EBECED;
		height: auto;
		padding: 32rpx;
		box-sizing: border-box;

		.list-box {
			background: #FFFFFF;
			border-radius: 20rpx;
			height: auto;

			.list {
				height: auto;
				border-bottom: 1px dashed #dcdcdc;
			}

			.list-btn {}
		}

	}

	.button-box {
		// position: absolute;
		width: 100%;
		bottom: 0;
		background: #fff;
		height: 100rpx;
	}
</style>